package com.sudao.android.ball;

import android.content.Context;
import android.content.res.Resources;
import android.os.RemoteException;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Map;

public class SeekBarPreference extends DialogPreference implements
        SeekBar.OnSeekBarChangeListener {

    private SeekBar mSeekBar;
	private TextView mMessageView;
    
    private int mMin = 0;
    private int mMax = 100;
    
    public static interface Callback {
    	int onSetInitialProgress();
    	String onSetMessage();
    	void onSaveProgress(int value);
    };
    
    private Callback mListener;

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.set_transparency);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);

        mSeekBar = getSeekBar(view);
        mSeekBar.setOnSeekBarChangeListener(this);
        mSeekBar.setMax(mMax - mMin);
        int progress = mListener.onSetInitialProgress(); 
        mSeekBar.setProgress(progress - mMin);
        
        mMessageView = getMessageView(view);
        mMessageView.setText(mListener.onSetMessage()+
        		progress);
    }
    
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
    	if(mMessageView != null)
    		mMessageView.setText(mListener.onSetMessage()+
    				progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        
        if (positiveResult) {
        	int progress = mSeekBar.getProgress() + mMin;
        	mListener.onSaveProgress(progress);
        }
    }

    protected static SeekBar getSeekBar(View dialogView) {
        return (SeekBar) dialogView.findViewById(R.id.seekbar);
    }

    protected static TextView getMessageView(View dialogView) {
        return (TextView) dialogView.findViewById(R.id.message);
    }

	public void setListener(Callback listener) {
		mListener = listener;
	}

	public void setMin(int min) {
		mMin = min;
	}

	public void setMax(int max) {
		mMax = max;
	}
}

